package com.hp.team.bookstore.dbutil.init;

import com.hp.team.bookstore.dbutil.dao.DAOFactory;
import com.hp.team.bookstore.dbutil.dao.HibernateUtil;
import com.hp.team.bookstore.dbutil.dao.catalog.VendorHibernateDAO;
import com.hp.team.bookstore.model.catalog.Vendor;

public class VendorInitializer {

	private static VendorHibernateDAO vendorDAO;

	static {
		DAOFactory daoFactory = new DAOFactory();
		vendorDAO = daoFactory.getVendorDAO();
	}

	public static void init() {

		vendorDAO.setSession(HibernateUtil.getSessionFactory().getCurrentSession());
		HibernateUtil.beginWriteTransaction();

		for (int i = 0; i < 10; i++) {
			Vendor vendor = createVendor(i);
			vendorDAO.makePersistent(vendor);
		}

		HibernateUtil.commitTransaction();
	}

	public static Vendor createVendor(long id) {
		Vendor vendor = new Vendor();
		vendor.setName("Vendor #" + id);
		vendor.setDescription("A description of vendor #" + id);
		return vendor;
	}

	public static void main(String[] args) {
		UserInitializer.init();
	}

}
